import {
  queryShopList,
  updateShop,
  queryShopDetail,
  uploadPhoto,
  changeShopStatus,
  createShopSort,
  queryShopSortList,
  queryShopSortDetail,
  updateShopSort,
  delShopSort,
  queryAllShopSortList,
  initShop,
  updateAllShop,
} from '@/services/shop';
import { message } from 'antd';
import router from 'umi/router';
export default {
  namespace: 'shops',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    shopDetail: {},
    shopSortDetail: {},
    imgList: [],
    shopSort: [],
  },

  effects: {
    *fetchSubmitShopForm({ payload }, { call, put }) {
      const response = yield call(initShop, payload);
      console.log(response);
    },
    *fetchShoplist({ payload }, { call, put }) {
      const response = yield call(queryShopList, payload.currentPage);
      yield put({
        type: 'save',
        payload: {
          list: response.data.data,
          pagination: {
            total: response.data.total,
            pageSize: 10,
            current: parseInt(response.data.currentPage, 10) || 1,
          },
        },
      });
    },
    *fetchShopdetail({ payload, callback }, { call, put }) {
      const response = yield call(queryShopDetail, payload);
      console.log(response);
      yield put({
        type: 'saveShopDetail',
        payload: response.data,
      });
      if (callback) callback(response.data.shop_coursel);
    },

    *fetchChangeShopStatus({ payload, callback }, { call, put }) {
      console.log(payload);
      const response = yield call(changeShopStatus, payload);
      if (callback) callback();
    },
    *uploadShopPhoto({ payload, callback }, { call, put }) {
      const response = yield call(uploadPhoto, payload);
      console.log(response);
      if (callback) callback(response.status, response.data);
    },
    *submitShopForm({ payload, callback }, { call, put }) {
      const response = yield call(updateShop, payload);
      if (response.status === 'success') {
        message.success('更新成功');
        if (callback) callback();
      } else {
        message.error('更新失败');
      }
    },
    *submitShopSortForm({ payload, callback }, { call, put }) {
      const response = yield call(createShopSort, payload);
      if (response.status === 'success') {
        message.success('添加成功');
        if (callback) callback();
      } else {
        message.error('添加失败');
      }
    },
    *submitShopSortUpateForm({ payload, callback }, { call, put }) {
      const response = yield call(updateShopSort, payload);
      if (response.status === 'success') {
        message.success('更新成功');
        if (callback) callback();
      } else {
        message.error('更新失败');
      }
    },
    *fetchShopSortList({ payload }, { call, put }) {
      const response = yield call(queryShopSortList, payload);
      yield put({
        type: 'save',
        payload: {
          list: response.data.data,
          pagination: {
            total: response.data.total,
            pageSize: 10,
            current: parseInt(response.data.currentPage, 10) || 1,
          },
        },
      });
    },
    *fetchAllShopSortList({ payload }, { call, put }) {
      const response = yield call(queryAllShopSortList, payload);
      yield put({
        type: 'saveAllShopSort',
        payload: response.data,
      });
    },
    *fetchShopSortForm({ call, put }) {
      const response = yield call(queryShopSortList);
      yield put({
        type: 'saveShopSort',
      });
    },
    *fetchShopSortdetail({ payload, callback }, { call, put }) {
      const response = yield call(queryShopSortDetail, payload);
      yield put({
        type: 'saveShopSortDetail',
        payload: response.data,
      });
      if (callback) callback(response.data.img);
    },
    *fetchDelShopSort({ payload, callback }, { call, put }) {
      const response = yield call(delShopSort, payload);
      if (callback) callback();
    },
    *fetchUpateAllShop({ payload, callback }, { call, put }) {
      const response = yield call(updateAllShop, payload);
      if (response.status === 'success') {
        message.success('更新成功');
        if (callback) callback();
      } else {
        message.error('更新失败');
      }
    },
  },

  reducers: {
    saveAllShopSort(state, action) {
      return {
        ...state,
        shopSort: action.payload,
      };
    },
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveShopDetail(state, action) {
      return {
        ...state,
        shopDetail: action.payload,
      };
    },
    saveShopSortDetail(state, action) {
      return {
        ...state,
        shopSortDetail: action.payload,
      };
    },
  },
};
